package com.sysolve.betterCity.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sysolve.betterCity.common.Help;
import com.sysolve.betterCity.model.Activity;
import com.sysolve.betterCity.service.BetterCityService;

/**
 * Servlet implementation class for Servlet: RetrieveActivity
 *
 */
 public class RetrieveActivity extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public RetrieveActivity() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String domain = request.getParameter("domain");
		String userIds = request.getParameter("userIds");
		BetterCityService service = BetterCityService.getInstance();
		
		response.setContentType("text/html");  
		response.setCharacterEncoding("utf-8");
		
        PrintWriter out = response.getWriter();
        
        List<Activity> list = service.findActivityByPersons(service.findPersons(domain, userIds), 20);
        if (list==null || list.isEmpty())
        	out.println("<div class=\"info\">目前还没有活动记录。</div>");
        else
        	for (Activity a: list) {
        		out.println("<li><img src=\""+ a.getActivityType().getIcon() +"\" alt=\""+ a.getActivityType().getTitle() +"\" /> ");
        		out.println(a.getOwner().getUsername());
        		out.println(" 于 "+Help.DATE_FORMAT.format(a.getEventTime()));
        		out.print(" 在 <a href=\"javascript:void(0);\" onclick=\"showPoint("+a.getPlace().getLat()+","+a.getPlace().getLng()+",'"+a.getActivityType().getIcon()+"','"+a.getTitle()+"')\">");
        		out.print(a.getPlace().getPlace());
        		out.println("</a> ");
        		out.println(a.getTitle());
        		out.println("</li>");
        	}
        
        out.close();
        

	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}   	  	    
}